package com.asktgapp.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.asktgapp.JGTool.Utils;
import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.dialog.CProgressDialogUtils;
import com.asktgapp.dialog.TokenInvalidDialog;
import com.asktgapp.hxsg.BiuEaseHelper;
import com.asktgapp.model.HomeServiceVO;
import com.asktgapp.model.SearchVO;
import com.asktgapp.modulebase.common.adapter.BaseAdapter;
import com.asktgapp.modulebase.common.adapter.BaseViewHolder;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.modulebase.common.util.Util;
import com.asktgapp.user.activity.AskDetialActivity;
import com.asktgapp.user.activity.EngineerDetailActivity;
import com.asktgapp.user.activity.FreeAskAnswerDetailActivity;
import com.asktgapp.user.activity.HomeServiceDetailActivity;
import com.asktgapp.user.activity.IndustryInfoDetailActivity;
import com.asktgapp.user.activity.LoginActivity;
import com.asktgapp.user.activity.QiuZuDetailActivity;
import com.asktgapp.user.activity.RentDetailActivity;
import com.asktgapp.user.activity.SearchMoreActivity;
import com.asktgapp.user.activity.UsedMachineDetailActivity;
import com.asktgapp.utils.ImageDisplayUtil;
import com.umeng.analytics.MobclickAgent;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018/1/23
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public class SearchBaseActivity extends AppCompatActivity implements View.OnClickListener {

    @InjectView(R.id.iv_back)
    ImageView mBackIcon;
    @InjectView(R.id.et_search)
    EditText mSearchEt;
    @InjectView(R.id.tv_search)
    TextView mSearchTv;
    @InjectView(R.id.iv_delete)
    ImageView delete;
    @InjectView(R.id.recycle)
    RecyclerView mRecyclerview;

    private BaseAdapter mBaseAdapter;
    private SearchVO mSearchVO;
    private Context mContext;
    @InjectView(R.id.tv_no_data)
    TextView mNoData;
    String key;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_search);
        ButterKnife.inject(this);
        init();
    }

    protected void init() {
        mContext = this;
        mSearchTv.setOnClickListener(this);
        mNoData = (TextView) findViewById(R.id.tv_no_data);
        mSearchEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    delete.setVisibility(View.VISIBLE);
                } else {
                    delete.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
//        mSearchEt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//                if (hasFocus) {
//                    mSearchEt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
//                } else {
//                    Resources resources = SearchBaseActivity.this.getResources();
//                    mSearchEt.setCompoundDrawablesWithIntrinsicBounds(resources.getDrawable(R.mipmap.ico_search), null, null, null);
//
//                }
//            }
//        });
        delete.setOnClickListener(this);
        mBackIcon.setOnClickListener(this);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mBaseAdapter = new BaseAdapter(this) {
            @Override
            public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
                BaseViewHolder holder = null;
                if (viewType == 0) {//工程师
                    holder = new BaseViewHolder(LayoutInflater.from(SearchBaseActivity.this).inflate(R.layout.item_search_projecter, parent, false),
                            new BaseViewHolder.Callbacks() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    RelativeLayout head = holder.getView(R.id.rl_head);
                                    TextView more = holder.getView(R.id.tv_home_more_question);
                                    if (data instanceof SearchVO.ProjecterListBean) {
                                        SearchVO.ProjecterListBean bean = (SearchVO.ProjecterListBean) data;
                                        if (bean.isFirst()) {
                                            head.setVisibility(View.VISIBLE);
                                            if (mSearchVO.getProjecter_more() == 1) {
                                                more.setVisibility(View.VISIBLE);
                                                more.setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View v) {
                                                        Intent intent = new Intent(mContext, SearchMoreActivity.class);
                                                        intent.putExtra("key", key);
                                                        intent.putExtra("type", 0);
                                                        startActivity(intent);
                                                    }
                                                });
                                            } else {
                                                more.setVisibility(View.GONE);
                                            }
                                        } else {
                                            head.setVisibility(View.GONE);
                                        }
                                        ImageView headImg = holder.getView(R.id.iv_head_engineer);
                                        TextView mTag1 = holder.getView(R.id.tv_tag1);
                                        TextView mTag2 = holder.getView(R.id.tv_tag2);
                                        ImageDisplayUtil.loadCircleHeadImage(SearchBaseActivity.this, headImg, bean.getUserPic());
                                        holder.setText(R.id.tv_reason, bean.getRecommendReason());
                                        holder.setText(R.id.tv_price, "¥ " + bean.getAdvisoryPrice() + "元");
                                        holder.setText(R.id.tv_name, bean.getRealname());
                                        if (Util.isEmpty(bean.getLabel())) {
                                            mTag1.setVisibility(View.GONE);
                                            mTag2.setVisibility(View.GONE);
                                        } else {
                                            String[] str;
                                            if (bean.getLabel().contains("，")) {
                                                str = bean.getLabel().split("，");
                                            } else {
                                                str = bean.getLabel().split(",");
                                            }
                                            if (str.length == 0) {
                                                mTag1.setVisibility(View.GONE);
                                                mTag2.setVisibility(View.GONE);
                                            } else if (str.length == 1) {
                                                if (Util.isEmpty(str[0])) {
                                                    mTag1.setVisibility(View.GONE);
                                                } else {
                                                    mTag1.setVisibility(View.VISIBLE);
                                                }
                                                mTag2.setVisibility(View.GONE);
                                                mTag1.setText(str[0]);
                                            } else if (str.length >= 2) {
                                                mTag1.setVisibility(View.VISIBLE);
                                                mTag2.setVisibility(View.VISIBLE);
                                                mTag1.setText(str[0]);
                                                mTag2.setText(str[1]);
                                            }
                                        }
                                    }
                                }

                                @Override
                                public void onItemClick(View view, int position) {
                                    SearchVO.ProjecterListBean bean = (SearchVO.ProjecterListBean) mBaseAdapter.getData().get(position);
                                    Intent intent = new Intent(mContext, EngineerDetailActivity.class);
                                    intent.putExtra("id", String.valueOf(bean.getId()));
                                    startActivity(intent);
                                }
                            });
                } else if (viewType == 1) {//问答
                    holder = new BaseViewHolder(LayoutInflater.from(SearchBaseActivity.this).inflate(R.layout.item_search_ask, parent, false),
                            new BaseViewHolder.Callbacks() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    RelativeLayout head = holder.getView(R.id.rl_head);
                                    TextView more = holder.getView(R.id.tv_home_more_question);
                                    RelativeLayout answer1 = holder.getView(R.id.rl_answer);
                                    TextView defaultText = holder.getView(R.id.tv_default);
                                    if (data instanceof SearchVO.ProblemListBean) {
                                        SearchVO.ProblemListBean bean = (SearchVO.ProblemListBean) data;
                                        if (bean.isFirst()) {
                                            head.setVisibility(View.VISIBLE);
                                            if (mSearchVO.getProblem_more() == 1) {
                                                more.setVisibility(View.VISIBLE);
                                                more.setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View v) {
                                                        Intent intent = new Intent(mContext, SearchMoreActivity.class);
                                                        intent.putExtra("key", key);
                                                        intent.putExtra("type", 1);
                                                        startActivity(intent);
                                                    }
                                                });
                                            } else {
                                                more.setVisibility(View.GONE);
                                            }
                                        } else {
                                            head.setVisibility(View.GONE);
                                        }
                                        ImageView Qhead = holder.getView(R.id.iv_head_ask);
                                        TextView name = holder.getView(R.id.tv_ask_name);
                                        TextView time = holder.getView(R.id.tv_ask_time);
                                        TextView question = holder.getView(R.id.tv_question);
                                        TextView answer = holder.getView(R.id.tv_answer);
                                        ImageView Ahead = holder.getView(R.id.iv_answer_head);
                                        TextView answerName = holder.getView(R.id.tv_answer_name);
                                        TextView zan = holder.getView(R.id.tv_zan);
                                        TextView label1 = holder.getView(R.id.tv_answer_label1);
                                        TextView label2 = holder.getView(R.id.tv_answer_label2);
                                        ImageDisplayUtil.loadCircleHeadImage(mContext, Qhead, bean.getQUserPic());
                                        //提问人昵称
                                        name.setText(bean.getQUersName());
                                        Date date = Util.StrToDate(bean.getAddTime(), "yyyy.MM.dd HH:mm:ss");
                                        String timeStr = Utils.getAskTime(date);
//                                        if (data != null)
//                                            timeStr = Util.DateToStr(date, "yyyy-MM-dd");
                                        time.setText(timeStr);
                                        question.setText(bean.getQContent());
                                        if (bean.getIsAnswer() == 1) {//已解答
                                            answer1.setVisibility(View.VISIBLE);
                                            defaultText.setVisibility(View.GONE);
                                            //提问人头像
                                            ImageDisplayUtil.loadCircleHeadImage(mContext, Ahead, bean.getAUserPic());
                                            answerName.setText(bean.getaRealName());
                                            answer.setText(bean.getAContent());
                                            zan.setText(String.valueOf(bean.getLikeNumber()));
                                            String[] lable = bean.getALabel().split(",");
                                            switch (lable.length) {
                                                case 0:
                                                    label1.setVisibility(View.GONE);
                                                    label2.setVisibility(View.GONE);
                                                    break;
                                                case 1:
                                                    if (Util.isEmpty(lable[0])) {
                                                        label1.setVisibility(View.GONE);
                                                    } else {
                                                        label1.setVisibility(View.VISIBLE);
                                                    }
                                                    label1.setText(lable[0]);
                                                    label2.setVisibility(View.GONE);
                                                    break;
                                                case 2:
                                                    label1.setVisibility(View.VISIBLE);
                                                    label1.setText(lable[0]);
                                                    label2.setVisibility(View.VISIBLE);
                                                    label2.setText(lable[1]);
                                                    break;
                                                default:
                                                    label1.setVisibility(View.VISIBLE);
                                                    label1.setText(lable[0]);
                                                    label2.setVisibility(View.VISIBLE);
                                                    label2.setText(lable[1]);
                                                    break;
                                            }

                                        } else {
                                            //未解答
                                            answer1.setVisibility(View.GONE);
                                            defaultText.setVisibility(View.VISIBLE);
                                        }
                                    }
                                }

                                @Override
                                public void onItemClick(View view, int position) {
                                    SearchVO.ProblemListBean bean = (SearchVO.ProblemListBean) mBaseAdapter.getData().get(position);
                                    FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty(String.valueOf(bean.getQId()),mContext);
//                                    if (bean.getIsAnswer() == 0 && PreferencesUtil.getBoolean(mContext, PreferencesUtil.KEY_IS_ENGINEER)) {
//                                        Intent intent = new Intent(mContext, FreeAskAnswerDetailActivity.class);
//                                        intent.putExtra("id", bean.getQId());
//                                        startActivity(intent);
//                                    } else {
//                                        Intent intent = new Intent(mContext, AskDetialActivity.class);
//                                        //// TOD: 2018/3/4  如果这里可以回答问题的话，需要判断是不是工程师，如果是跳回答问题界面
//                                        intent.putExtra("id", String.valueOf(bean.getQId()));
//                                        startActivity(intent);
//                                    }
                                }
                            });

                } else if (viewType == 2) {//二手机
                    holder = new BaseViewHolder(LayoutInflater.from(SearchBaseActivity.this).inflate(R.layout.item_search_towhard, parent, false),
                            new BaseViewHolder.Callbacks() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    RelativeLayout head = holder.getView(R.id.rl_head);
                                    TextView more = holder.getView(R.id.tv_home_more_question);
                                    if (data instanceof SearchVO.TwoHardListBean) {
                                        SearchVO.TwoHardListBean bean = (SearchVO.TwoHardListBean) data;
                                        if (bean.isFirst()) {
                                            head.setVisibility(View.VISIBLE);
                                            if (mSearchVO.getTwoHard_more() == 1) {
                                                more.setVisibility(View.VISIBLE);
                                                more.setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View v) {
                                                        Intent intent = new Intent(mContext, SearchMoreActivity.class);
                                                        intent.putExtra("key", key);
                                                        intent.putExtra("type", 2);
                                                        startActivity(intent);
                                                    }
                                                });
                                            } else {
                                                more.setVisibility(View.GONE);
                                            }
                                        } else {
                                            head.setVisibility(View.GONE);
                                        }
                                        ImageView saleState = holder.getView(R.id.iv_saled);
                                        ImageView img = holder.getView(R.id.iv_machine);
                                        ImageDisplayUtil.LoadNetImageWithUrl(img, mContext, bean.getBanner_pic());
                                        holder.setText(R.id.tv_used_machine_name, bean.getTitle());
                                        holder.setText(R.id.detail, Utils.getDateYear(bean.getProduction_date()) +
                                                " | " + Utils.D2I(bean.getWorking_hours()) + "小时 | " + bean.getPro_name().replaceAll("省", "") + " " + bean.getCity_name().replaceAll("市", ""));
                                        DecimalFormat df = new DecimalFormat("0.00");
                                        holder.setText(R.id.tv_price, df.format(bean.getPrice()) + bean.getPrice_unit() + "");
                                        if (bean.getState() == 0) {
                                            saleState.setVisibility(View.GONE);
                                        } else {
                                            saleState.setVisibility(View.VISIBLE);
                                        }
                                    }
                                }

                                @Override
                                public void onItemClick(View view, int position) {
                                    SearchVO.TwoHardListBean bean = (SearchVO.TwoHardListBean) mBaseAdapter.getData().get(position);
                                    Intent intent = new Intent(mContext, UsedMachineDetailActivity.class);
                                    intent.putExtra("id", String.valueOf(bean.getId()));
                                    intent.putExtra("unit", bean.getPrice_unit());
                                    startActivity(intent);
                                }
                            });

                } else if (viewType == 3) {//租赁
                    holder = new BaseViewHolder(LayoutInflater.from(SearchBaseActivity.this).inflate(R.layout.item_search_lease, parent, false),
                            new BaseViewHolder.Callbacks() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    RelativeLayout head = holder.getView(R.id.rl_head);
                                    TextView more = holder.getView(R.id.tv_home_more_question);
                                    if (data instanceof SearchVO.LeaseListBean) {
                                        SearchVO.LeaseListBean bean = (SearchVO.LeaseListBean) data;
                                        if (bean.isFirst()) {
                                            head.setVisibility(View.VISIBLE);
                                            if (mSearchVO.getLease_more() == 1) {
                                                more.setVisibility(View.VISIBLE);
                                                more.setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View v) {
                                                        Intent intent = new Intent(mContext, SearchMoreActivity.class);
                                                        intent.putExtra("key", key);
                                                        intent.putExtra("type", 3);
                                                        startActivity(intent);
                                                    }
                                                });
                                            } else {
                                                more.setVisibility(View.GONE);
                                            }
                                        } else {
                                            head.setVisibility(View.GONE);
                                        }
                                        ImageView state = holder.getView(R.id.iv_state);
                                        ImageView img = holder.getView(R.id.iv_machine);
                                        holder.setText(R.id.tv_used_machine_name, bean.getTitle());
                                        holder.setText(R.id.tv_price, Utils.D2I(bean.getPrice()) + bean.getPrice_unit() + "");
                                        String pro = bean.getPro_name().replaceAll("省", "");
                                        String city = bean.getCity_name().replaceAll("市", "");
                                        ImageDisplayUtil.LoadNetImageWithUrl(img, mContext, bean.getBanner_pic());
//                                        if (pro.equals(city)) {
//                                            holder.setText(R.id.tv_detail, Utils.getDateAll(bean.getAdd_time())
//                                                    + " | " + pro);
//
//                                        } else {
                                        holder.setText(R.id.tv_detail, Utils.getDateYMD(bean.getAdd_time())
                                                + " | " + pro + " " + city);
//                                        }
                                        //出租0:待售 1:已售 求租没有
                                        state.setVisibility(View.VISIBLE);
                                        if (bean.getState() == 0) {
                                            state.setImageResource(R.mipmap.bg_rent);
                                        } else {
                                            state.setImageResource(R.mipmap.bg_busy);
                                        }
                                    }
                                }

                                @Override
                                public void onItemClick(View view, int position) {
                                    SearchVO.LeaseListBean bean = (SearchVO.LeaseListBean) mBaseAdapter.getData().get(position);
                                    Intent intent = new Intent(mContext, RentDetailActivity.class);
                                    intent.putExtra("id", String.valueOf(bean.getId()));
                                    intent.putExtra("unit", bean.getPrice_unit());
                                    startActivity(intent);
                                }
                            });

                } else if (viewType == 4) {//咨讯
                    holder = new BaseViewHolder(LayoutInflater.from(SearchBaseActivity.this).inflate(R.layout.item_search_info, parent, false),
                            new BaseViewHolder.Callbacks() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    RelativeLayout head = holder.getView(R.id.rl_head);
                                    TextView more = holder.getView(R.id.tv_home_more_question);
                                    ImageView img = holder.getView(R.id.iv_industry_img);
                                    TextView title = holder.getView(R.id.tv_industry_title);
                                    TextView time = holder.getView(R.id.tv_industry_time);
                                    TextView comment = holder.getView(R.id.tv_industry_read_number);
                                    TextView like = holder.getView(R.id.tv_industry_like);
                                    if (data instanceof SearchVO.InfoListBean) {
                                        SearchVO.InfoListBean bean = (SearchVO.InfoListBean) data;
                                        if (bean.isFirst()) {
                                            head.setVisibility(View.VISIBLE);
                                            if (mSearchVO.getInfo_more() == 1) {
                                                more.setVisibility(View.VISIBLE);
                                                more.setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View v) {
                                                        Intent intent = new Intent(mContext, SearchMoreActivity.class);
                                                        intent.putExtra("key", key);
                                                        intent.putExtra("type", 5);
                                                        startActivity(intent);
                                                    }
                                                });
                                            } else {
                                                more.setVisibility(View.GONE);
                                            }
                                        } else {
                                            head.setVisibility(View.GONE);
                                        }
                                        ImageDisplayUtil.LoadNetImage(img, mContext, bean.getBannerPic());
                                        title.setText(bean.getTitle());
                                        time.setText(Utils.getDateYMD(bean.getAddTime()));
                                        comment.setText(String.valueOf(bean.getCommentNumber()));
                                        like.setText(String.valueOf(bean.getLikeNumber()));
                                    }
                                }

                                @Override
                                public void onItemClick(View view, int position) {
                                    SearchVO.InfoListBean bean = (SearchVO.InfoListBean) mBaseAdapter.getData().get(position);
                                    Intent intent = new Intent(mContext, IndustryInfoDetailActivity.class);
                                    intent.putExtra("id", String.valueOf(bean.getId()));
                                    startActivity(intent);
                                }
                            });

                } else if (viewType == 5) {
                    holder = new BaseViewHolder(LayoutInflater.from(SearchBaseActivity.this).inflate(R.layout.item_search_qiuzu, parent, false), new BaseViewHolder.Callbacks() {
                        @Override
                        public void bind(BaseViewHolder holder, Object data) {
                            RelativeLayout head = holder.getView(R.id.rl_head);
                            TextView more = holder.getView(R.id.tv_home_more_question);
                            if (data instanceof SearchVO.QiuZuListBean) {
                                SearchVO.QiuZuListBean bean = (SearchVO.QiuZuListBean) data;
                                if (bean.isFirst()) {
                                    head.setVisibility(View.VISIBLE);
                                    if (mSearchVO.getQiuZu_more() == 1) {
                                        more.setVisibility(View.VISIBLE);
                                        more.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                Intent intent = new Intent(mContext, SearchMoreActivity.class);
                                                intent.putExtra("key", key);
                                                intent.putExtra("type", 4);
                                                startActivity(intent);
                                            }
                                        });
                                    } else {
                                        more.setVisibility(View.GONE);
                                    }
                                } else {
                                    head.setVisibility(View.GONE);
                                }

                                holder.setText(R.id.tv_qiuzu_title, bean.getTitle());
                                holder.setText(R.id.tv_price, Utils.D2I(bean.getPrice()) + bean.getPrice_unit() + "");
                                String pro = bean.getPro_name().replaceAll("省", "");
                                String city = bean.getCity_name().replaceAll("市", "");
//                                if (pro.equals(city)) {
//                                    holder.setText(R.id.tv_info, Utils.getDateAll(bean.getAdd_time())
//                                            + " | " + pro + " | "
//                                            + "工期" + bean.getTime_limit() + "天");
//                                } else
                                holder.setText(R.id.tv_info, Utils.getDateYMD(bean.getAdd_time())
                                        + " | " + pro + " "
                                        + city + " | "
                                        + "工期" + bean.getTime_limit() + "天");
                            }


                        }

                        @Override
                        public void onItemClick(View view, int position) {
                            SearchVO.QiuZuListBean bean = (SearchVO.QiuZuListBean) mBaseAdapter.getData().get(position);
                            Intent intent = new Intent(SearchBaseActivity.this, QiuZuDetailActivity.class);
                            intent.putExtra("id", String.valueOf(bean.getId()));
                            startActivity(intent);
                        }
                    });
                } else if (viewType == 6) {//上门服务
                    holder = new BaseViewHolder(LayoutInflater.from(SearchBaseActivity.this).inflate(R.layout.item_home_service, parent, false),
                            new BaseViewHolder.Callbacks() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    if (data instanceof SearchVO.ServiceListBean) {
                                        ImageView userHead = holder.getView(R.id.userHead);
                                        RelativeLayout head = holder.getView(R.id.rl_head);
                                        TextView more = holder.getView(R.id.tv_home_more_question);
                                        SearchVO.ServiceListBean bean = (SearchVO.ServiceListBean) data;
                                        if (bean.isFirst()) {
                                            head.setVisibility(View.VISIBLE);
                                            if (mSearchVO.getService_more() == 1) {
                                                more.setVisibility(View.VISIBLE);
                                                more.setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View v) {
                                                        Intent intent = new Intent(mContext, SearchMoreActivity.class);
                                                        intent.putExtra("key", key);
                                                        intent.putExtra("type", 6);
                                                        startActivity(intent);
                                                    }
                                                });
                                            } else {
                                                more.setVisibility(View.GONE);
                                            }
                                        } else {
                                            head.setVisibility(View.GONE);
                                        }
                                        if (mBaseAdapter.getItemIndex(bean) == 0)
                                            holder.getView(R.id.tvLine).setVisibility(View.VISIBLE);
                                        else
                                            holder.getView(R.id.tvLine).setVisibility(View.GONE);
                                        userHead.setVisibility(View.VISIBLE);
                                        holder.getView(R.id.userName).setVisibility(View.VISIBLE);
                                        holder.getView(R.id.serviceType).setVisibility(View.GONE);
                                        holder.setText(R.id.userName, bean.getUserName());
                                        holder.setText(R.id.serviceContent, bean.getInfo());
                                        Date date = Util.StrToDate(bean.getAddTime(), "yyyy.MM.dd HH:mm:ss");
                                        String timeStr = Utils.getAskTime(date);
                                        holder.setText(R.id.addTime, timeStr);
                                        String pro = bean.getProviceName().replaceAll("省", "");
                                        String city = bean.getCityName().replaceAll("市", "");
                                        if (TextUtils.isEmpty(pro + " " + city))
                                            holder.setText(R.id.serviceAddress, "未知地区");
                                        else
                                            holder.setText(R.id.serviceAddress, pro + " " + city);
                                        holder.getView(R.id.serviceStatus).setVisibility(View.GONE);
//                                    ImageDi holder.getView(R.id.serviceStatus).setVisibility(View.GONE);splayUtil.LoadNetImageWithUrl(userHead, getActivity(), bean.getUserPic());
                                        ImageDisplayUtil.loadCircleHeadImage(SearchBaseActivity.this, userHead, bean.getUserPic());
                                        String type = bean.getType();
                                        String serviceType = "维修";
                                        if (type.equals("1")) {
                                            serviceType = ("维修");
                                        } else if (type.equals("2")) {
                                            serviceType = ("检车");
                                        } else if (type.equals("3")) {
                                            serviceType = ("保养");
                                        } else if (type.equals("4")) {
                                            serviceType = ("评估");
                                        }
                                        holder.getView(R.id.serviceTypeTop).setVisibility(View.VISIBLE);
                                        holder.setText(R.id.serviceTypeTop, serviceType);

                                    }
                                }

                                @Override
                                public void onItemClick(View view, int position) {
                                    if (Util.isEmpty(PreferencesUtil.getString(SearchBaseActivity.this, PreferencesUtil.USER_ID))) {
                                        Intent intent = new Intent(SearchBaseActivity.this, LoginActivity.class);
                                        startActivity(intent);
                                    }else {
                                        SearchVO.ServiceListBean bean = (SearchVO.ServiceListBean) mBaseAdapter.getData().get(position);
                                        Intent intent = new Intent(SearchBaseActivity.this, HomeServiceDetailActivity.class);
                                        intent.putExtra("id", bean.getId());
//                                intent.putExtra("unit", bean.getPrice_unit());
                                        startActivity(intent);
                                    }
                                }
                            });
                }
                return holder;
            }

            @Override
            public int getItemViewType(int position) {
                Object bean = mBaseAdapter.getData().get(position);
                if (bean instanceof SearchVO.ProjecterListBean) {
                    return 0;
                }
                if (bean instanceof SearchVO.ProblemListBean)
                    return 1;
                if (bean instanceof SearchVO.TwoHardListBean)
                    return 2;
                if (bean instanceof SearchVO.LeaseListBean)
                    return 3;
                if (bean instanceof SearchVO.InfoListBean)
                    return 4;
                if (bean instanceof SearchVO.QiuZuListBean)
                    return 5;
                if (bean instanceof SearchVO.ServiceListBean)
                    return 6;
                return 0;
            }

        };
        mRecyclerview.setAdapter(mBaseAdapter);


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.reset(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_delete:
                mSearchEt.setText("");
                break;
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.tv_search:
                doSearch();
                break;
        }
    }

    private void doSearch() {
        hideSoftKeyboard();
        key = mSearchEt.getText().toString();
        if (Util.isEmpty(key)) {
            Toast.makeText(this, "请输入搜索内容", Toast.LENGTH_SHORT).show();
            return;
        }
        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("keyWord", key);
        if (!Utils.noContainsEmoji(key)) {
            Toast.makeText(mContext, "不支持输入emoji表情", Toast.LENGTH_SHORT).show();
            return;
        }
        //与后台约定写1；
        params.put("page", 1);
        params.put("userid", PreferencesUtil.getString(getApplicationContext(), PreferencesUtil.USER_ID));
        CProgressDialogUtils.showProgressDialog(this);
        Call<ApiResponseBody<SearchVO>> call = apiservice.findList(params);
        call.enqueue(new Callback<ApiResponseBody<SearchVO>>() {
            @Override
            public void onResponse(Call<ApiResponseBody<SearchVO>> call, Response<ApiResponseBody<SearchVO>> response) {
                if (response.isSuccessful()) {
                    mSearchVO = response.body().getResult();
                    setUI(mSearchVO);
                    CProgressDialogUtils.cancelProgressDialog();
                } else {
                    CProgressDialogUtils.cancelProgressDialog();
                    Toast.makeText(SearchBaseActivity.this, response.raw().message(), Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody<SearchVO>> call, Throwable t) {
                CProgressDialogUtils.cancelProgressDialog();
                ApiException apiException = ExceptionHandle.handleException(t);
                Toast.makeText(SearchBaseActivity.this, apiException.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });


    }

    private void setUI(SearchVO bean) {
        List list = new ArrayList();
        if (bean.getProjecterList().size() > 0) {
            bean.getProjecterList().get(0).setFirst(true);
            list.addAll(bean.getProjecterList());
        }
        if (bean.getProblemList().size() > 0) {
            bean.getProblemList().get(0).setFirst(true);
            list.addAll(bean.getProblemList());
        }
        if (bean.getTwoHardList().size() > 0) {
            bean.getTwoHardList().get(0).setFirst(true);
            list.addAll(bean.getTwoHardList());
        }
        if (bean.getLeaseList().size() > 0) {
            bean.getLeaseList().get(0).setFirst(true);
            list.addAll(bean.getLeaseList());
        }
        if (bean.getInfoList().size() > 0) {
            bean.getInfoList().get(0).setFirst(true);
            list.addAll(bean.getInfoList());
        }
        if (bean.getQiuZuList().size() > 0) {
            bean.getQiuZuList().get(0).setFirst(true);
            list.addAll(bean.getQiuZuList());
        }
        if (bean.getServiceList().size() > 0) {
            bean.getServiceList().get(0).setFirst(true);
            list.addAll(bean.getServiceList());
        }
        if (list.size() > 0) {
            mBaseAdapter.setData(list);
            mNoData.setVisibility(View.GONE);
        } else {
            mNoData.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏软键盘
     */
    public void hideSoftKeyboard() {
        InputMethodManager inputManager = (InputMethodManager) mContext.getSystemService(Context
                .INPUT_METHOD_SERVICE);
        if (inputManager.isActive() && SearchBaseActivity.this.getCurrentFocus() != null) {
            inputManager.hideSoftInputFromWindow(SearchBaseActivity.this.getCurrentFocus().getWindowToken(), 0);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}

